#include <iostream>
#include "igslib_iutil.h"
#include "igslib_iserial_manager.h"
#include "igslib_imeter.h"
#include "igslib_imeter.h"

namespace igslib
{
    namespace meter_sample
    {
        /*!
         * This sample demostrates manipulating the meter.
         */
        static void add_value()
        {
            IGPIO* gpio_coin = nullptr;
            IGPIO* gpio_ticket = nullptr;
            ISpi* spi = nullptr;
            IGPIOMeter* mt_coin = nullptr;
            IGPIOMeter* mt_ticket = nullptr;
            int mtv_coin = 0; // meter value for coin
            int mtv_ticket = 0; // meter value for ticket

            {
                ISerialManager* mgr = GetSerialManagerInstance();
                spi = mgr->New_ProjMCU_spi("DragonBallRace");	
                spi->SetWorkingFrequency(10000000);
                bool open_result = spi->Open(ISpi::cs0);
                if (open_result == false)
                {
                    throw_igslib_exception("SPI open fail.");
                }
            }

            // coin meter
            {
                IGPIOManager* mgr = GetGPIOManagerInstance();
                gpio_coin = mgr->New_ProjMCU("DragonBallRace");
                gpio_coin->SetSPI(spi);
				gpio_coin->SetMode(IGPIO::mode::output);
				gpio_coin->SetNumber(8);
            }

            {
                IMeterManager* mgr = GetMeterManagerInstance();
                mt_coin = mgr->New_ProjGPIOMeter("DragonBallRace");
                mt_coin->SetSPI(spi);
                mt_coin->SetGPIO(gpio_coin);
                mt_coin->SetDutyRange(50,450);
            }

            // ticket meter
            {
                IGPIOManager* mgr = GetGPIOManagerInstance();
                gpio_ticket = mgr->New_ProjMCU("DragonBallRace");
                gpio_ticket->SetSPI(spi);
				gpio_ticket->SetMode(IGPIO::mode::output);
				gpio_ticket->SetNumber(9);
            }

            {
                IMeterManager* mgr = GetMeterManagerInstance();
                mt_ticket = mgr->New_ProjGPIOMeter("DragonBallRace");
                mt_ticket->SetSPI(spi);
                mt_ticket->SetGPIO(gpio_ticket);
                mt_ticket->SetDutyRange(50,450);
            }

            while(true)
            {
                auto kh = util::KbHit();
                if (kh == 27) // 'esc'
                {
                    break;
                }
                else if (kh == 97) // 'a'
                {
                    int value = 1;
                    mtv_coin += value;
                    mt_coin->AddValue(value);
                    util::Log("%s> mt_coin=%d\n",__func__,mtv_coin);
                }
                else if (kh == 98) // 'b'
                {
                    int value = 5;
                    mtv_ticket += value;
                    mt_ticket->AddValue(value);
                    util::Log("%s> mt_ticket=%d\n",__func__,mtv_ticket);
                }
                spi->Update();
                util::SleepMSTime(16);
            }
            exit(EXIT_SUCCESS);
        }
    };
};
